from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
class Login_Page():

    username = (By.CSS_SELECTOR,'#username')
    password = (By.CSS_SELECTOR,'#password')
    login_btn = (By.CSS_SELECTOR,'#loginBtn')

    
    def __init__(self):
        self.wd = webdriver.Chrome(service=Service(r'/Users/show/Downloads/编程/python学习/chromedriver-mac-arm64/chromedriver'))
        self.wait = WebDriverWait(self.wd,10)

    def load(self):
        self.wd.get('http://127.0.0.1:8234')
        print(self.wd.title)
        return self

    def username_click(self):
        
        ele1 = self.wait.until(EC.presence_of_element_located(self.username))
        ele1.clear()
        ele1.send_keys('byhy')
        return self
    
    def password_click(self):
        ele1 = self.wait.until(EC.presence_of_element_located(self.password))
        ele1.clear()
        ele1.send_keys('sdfsdf')
        return self
    
    def login_btn_click(self):
        ele1 = self.wait.until(EC.presence_of_element_located(self.login_btn))
        ele1.click()
        return self
    
    def login(self):
        self.load().username_click().password_click().login_btn_click()



    @classmethod
    def wait_(cls,driver,func):
        return WebDriverWait(driver,5).until(func)

    def find_element(self,by,value):
        #定义判断函数
        #这里until调用的时候就会传入driver，是在初始化WebDeriverWait时传入的
        def f(driver):
            ele = driver.find_element(by,value)

            #下面的都不用写，有返回值才写，没有返回值可以只写上面
            # if ele:
            #     return True
            # else:
            #     return False
        #显示等待
        self.wait_(self.wd,f)

        return self.wd.find_element(by,value)

l = Login_Page()
# l.login()
# time.sleep(1)
# print(l.wd.title)
# l.wd.quit()
# lo = (By.ID,'usernas')
# l.load().find_element(*lo).send_keys('caipeian')


l.wd.get('http://127.0.0.1:8234/login.html')
print(l.wd.title)